package com.shuai.controller;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.shuai.project.model.AvatarParams;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;

/**
 * @ClassName AvatarController
 * @Description TODO 
 * @Date 2023/5/9 0:37
 */
@RestController
@RequestMapping("/avatar")
public class AvatarController {


    @PostMapping("/avatarUrl")
    public String getAvatarUrlByPost(@RequestBody(required = false) AvatarParams avatarParams) throws Exception {
        String avatarUrl = "https://www.loliapi.com/acg/pp/";
        String redirectUrl = getRedirectUrl(avatarUrl);
        return redirectUrl;
    }


    /**
     * 获取重定向地址
     * @param path
     * @return
     * @throws Exception
     */
    private String getRedirectUrl(String path) throws Exception {
        HttpURLConnection conn = (HttpURLConnection) new URL(path)
                .openConnection();
        conn.setInstanceFollowRedirects(false);
        conn.setConnectTimeout(5000);
        String location = conn.getHeaderField("Location");
        return location;
    }

}
